﻿using System.Collections.Generic;
using BankCreditPortfolio.DAL.Entities;
using System.Linq;
using NHibernate;

namespace BankCreditPortfolio.DAL.Services
{
    public class TariffService : DataService<TariffPersistence>
    {
        //public Tariff GetTariffByTitle(string tariffTitle)
        //{
        //    var filteredQuery = this.CreateQuery().Where(x => x.Title == tariffTitle);
        //    return filteredQuery.Count() > 0 ? filteredQuery.Single() : null;
        //}
        
        new public Tariff Find(int id)
        {
            var objTmpQuery = this.CreateQuery().Where(x => x.TariffId == id);
            if (objTmpQuery.Count() <= 0) return null;
            var objTmp = objTmpQuery.First();
            if (objTmp == null) return null;
            var obj = GetTariffObject(objTmp);
            var tariffsDocumentsService = new TariffsDocumentsService();
            var documentService = new DocumentTypeService();
            var documnetsQuery = tariffsDocumentsService.CreateQuery().Where(x => x.TariffId == id);
            foreach (var document in documnetsQuery)
            {
                obj.Documents.Add(documentService.Find(document.DocumentTypeId));
            }
            return obj;
        }

        public Tariff GetByTitle(string title)
        {
            var result = this.CreateQuery().Where(x => x.Title == title);
            return result.Count() > 0 ? result.First() : null;
        }

        private Tariff GetTariffObject(TariffPersistence obj)
        {
            var result = new Tariff
                             {
                                 TariffId = obj.TariffId,
                                 CreditPercent = obj.CreditPercent,
                                 Currency = obj.Currency,
                                 Description = obj.Description,
                                 FirstPayment = obj.FirstPayment,
                                 Max = obj.Max,
                                 Min = obj.Min,
                                 NumberOfGuarantors = obj.NumberOfGuarantors,
                                 OtherPayments = obj.OtherPayments,
                                 Time = obj.Time,
                                 Title = obj.Title,
                                 Fine = obj.Fine,
                                 EarlyRepaymentFine = obj.EarlyRepaymentFine,
                                 Used = obj.Used
                             };
            return result;
        }

        new public IEnumerable<Tariff> GetAll()
        {
            var tmpCollectionBase =  base.GetAll();
            var tmpCollection = new List<Tariff>();
            foreach (var tariffPersistence in tmpCollectionBase)
            {
                tmpCollection.Add(GetTariffObject(tariffPersistence));
            }
            var tariffsDocumentsService = new TariffsDocumentsService();
            var documentService = new DocumentTypeService();
            foreach (var tariff in tmpCollection)
            {
                foreach (var document in tariffsDocumentsService.CreateQuery().Where(x => x.TariffId == tariff.TariffId))
                {
                    tariff.Documents.Add(documentService.Find(document.DocumentTypeId));
                }
            }
            return tmpCollection;
        }

        public override bool Save(ref TariffPersistence entity)
        {
            var obj = GetTariffObject(entity);
            var result = this.Save(ref obj);
            entity.TariffId = obj.TariffId;
            return result;
        }

        public bool Save(ref Tariff entity)
        {
            var obj = new TariffPersistence
                          {
                              TariffId = entity.TariffId,
                              CreditPercent = entity.CreditPercent,
                              Currency = entity.Currency,
                              Description = entity.Description,
                              FirstPayment = entity.FirstPayment,
                              Max = entity.Max,
                              Min = entity.Min,
                              NumberOfGuarantors = entity.NumberOfGuarantors,
                              OtherPayments = entity.OtherPayments,
                              Time = entity.Time,
                              Title = entity.Title,
                              Fine = entity.Fine,
                              EarlyRepaymentFine = entity.EarlyRepaymentFine,
                              Used = entity.Used
                          };

            var result = base.Save(ref obj);
            if (entity.Documents.Count > 0)
            {
                foreach (var documentTmp in entity.Documents)
                {
                    var document = documentTmp;
                    var tariffsDocumentsService = new TariffsDocumentsService();
                    var documentTypeService = new DocumentTypeService();
                    if (documentTypeService.Find(document.DocumentTypesId) == null)
                    {
                        documentTypeService.Save(ref document);
                    }
                    var relation = new TariffsDocuments {TariffId = obj.TariffId, DocumentTypeId = document.DocumentTypesId};
                    if (!tariffsDocumentsService.Find(relation))
                        tariffsDocumentsService.Save(ref relation);
                }
            }
            entity.TariffId = obj.TariffId;
            return result;
        }

        public bool Delete(Tariff entity)
        {
            var tariffsDocumentsService = new TariffsDocumentsService();
            foreach (var relation in tariffsDocumentsService.CreateQuery().Where(x => x.TariffId == entity.TariffId))
            {
                tariffsDocumentsService.Delete(relation);
            }
            return base.Delete(base.Find(entity.TariffId));
        }

        new public IQueryable<Tariff> CreateQuery()
        {
            var tmpCollection1 = base.CreateQuery();
            var tmpCollection = new List<Tariff>();
            var tariffsDocumentsService = new TariffsDocumentsService();
            var documentService = new DocumentTypeService();
            foreach (var tariffPersistence in tmpCollection1)
            {
                tmpCollection.Add(GetTariffObject(tariffPersistence));
            }
            foreach (var tariff in tmpCollection)
            {
                foreach (var document in tariffsDocumentsService.CreateQuery().Where(x => x.TariffId == tariff.TariffId))
                {
                    tariff.Documents.Add(documentService.Find(document.DocumentTypeId));
                }
            }
            return tmpCollection.AsQueryable();
        }
    }
}